//
//  HMDiscoverySearchView.swift
//  weibo
//
//  Created by LCH on 16/3/3.
//  Copyright © 2016年 lch. All rights reserved.
//

import UIKit

class HMDiscoverySearchView: UIView {

    @IBOutlet weak var textFieldRightCons: NSLayoutConstraint!
   
    @IBOutlet weak var textField: UITextField!
    
    @IBOutlet weak var cancelButton: UIButton!
    
    class func searchView() -> HMDiscoverySearchView {
    
      return NSBundle.mainBundle().loadNibNamed("HMDiscoverySearchView", owner: nil, options: nil).last! as!HMDiscoverySearchView
    }
    
    override func awakeFromNib() {
        
        leftView.frame.size = CGSize(width:self.frame.height, height:self.frame.height)
        
        textField.leftView = leftView
        
        textField.leftViewMode = .Always
        
        textField.layer.borderWidth = 1
        textField.layer.borderColor = UIColor.redColor().CGColor
        textField.layer.cornerRadius = 5
        textField.layer.masksToBounds = true
        
    }
    
    
    
    
    // 点击之后取消按钮不能整个显示
    @IBAction func editingDidBegin(sender: AnyObject) {
        
      print("开始编辑了")
      
     self.textFieldRightCons.constant = self.cancelButton.bounds.width
        
     UIView.animateWithDuration(0.25) { () -> Void in
        
        self.textField.layoutIfNeeded()
        }
        
       
        
        
    }
    
    @IBAction func cancleButtonClick(sender: AnyObject) {
        
        print("编辑按钮点击")
        
        textFieldRightCons.constant = 0
        
        textField.resignFirstResponder()
        
        UIView.animateWithDuration(0.25) { () -> Void in
            
            self.textField.layoutIfNeeded()
        }
        
    }
    
    private lazy var leftView: UIImageView = {
    
       let view = UIImageView(image:UIImage(named: "searchbar_textfield_search_icon"))
        
       view.contentMode = .Center
        
        return view
    
    }()
    
}
